package com.eralink.unionuhfreader.beep;

import android.content.Context;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;

import com.eralink.unionuhfreader.R;

public class Beep
{
    private static SoundPool soundPool = null;
    private static int sound_errID = 0;
    private static int sound_okID = 0;
    private static int stream_beepID=0;
    private static boolean bRunning;

    public static void init(Context ct)
    {
        bRunning = false;
        //if(soundPool == null)
        {
            AudioAttributes.Builder audioAttributesBuilder = new AudioAttributes.Builder();
            audioAttributesBuilder.setLegacyStreamType(AudioManager.STREAM_MUSIC);
            SoundPool.Builder soundPoolBuilder = new SoundPool.Builder();
            soundPoolBuilder.setMaxStreams(1);
            soundPoolBuilder.setAudioAttributes(audioAttributesBuilder.build());
            soundPool = soundPoolBuilder.build();
            sound_okID = soundPool.load(ct, R.raw.ding, 1);
            sound_errID = soundPool.load(ct, R.raw.beep_err, 1);
        }
    }

    public static void release()
    {
        if (stream_beepID > 0) {
            soundPool.stop(stream_beepID);
        }
        soundPool.unload(sound_errID);
        soundPool.unload(sound_okID);
        soundPool.release();
    }

    public static void playOK()
    {
        if (bRunning) {
            return;
        }
        if (soundPool == null) {
            return;
        }
        bRunning = true;
        if (stream_beepID > 0) {
            soundPool.stop(stream_beepID);
        }
        stream_beepID = soundPool.play(sound_okID , 1.0f, 1.0f, 0, 0, 1.0f);
        bRunning = false;
    }

    public static void playErr()
    {
        if (bRunning) {
            return;
        }
        if (soundPool == null) {
            return;
        }
        bRunning = true;
        if(stream_beepID > 0) {
            soundPool.stop(stream_beepID);
        }
        stream_beepID = soundPool.play(sound_errID , 1.0f, 1.0f, 0, 0, 1.0f);
        bRunning = false;
    }
}